//
//  MissPaparazziTests.m
//  MissPaparazziTests
//
//  Created by Alexey Aleshkov on 02.09.12.
//  Copyright (c) 2012 DzeTech. All rights reserved.
//


#import "MissPaparazziTests.h"
#import "ZipArchive+BlocksHelper.h"


#define ARCHIVE_FILE_NAME @"archive.zip"
#define ARCHIVE_PASS_FILE_NAME @"archive_pass.zip"

#define ARCHIVE_DEST_FOLDER @"archive"
#define ARCHIVE_PASS_DEST_FOLDER @"archive_pass"

#define TestResourceDirectory [[NSBundle bundleForClass:[self class]] resourcePath]


@implementation MissPaparazziTests

- (void)setUp
{
	[super setUp];

	_timeout = 30;
	_fileManager = [[NSFileManager alloc] init];
}

- (void)tearDown
{
	[_fileManager release];

	[super tearDown];
}

- (void)testCkeckFilesExistans
{
	NSString *archiveFilePath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_FILE_NAME];
	NSString *archivePassFilePath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_PASS_FILE_NAME];

	STAssertTrue([_fileManager fileExistsAtPath:archiveFilePath], @"archive.zip not exist");
	STAssertTrue([_fileManager fileExistsAtPath:archivePassFilePath], @"archive_pass.zip not exist");
}

- (void)testUnarchiveFile
{
	NSString *archiveFilePath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_FILE_NAME];
	NSString *archiveFileDestPath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_DEST_FOLDER];
	
	NSString *unarchivedFile = [archiveFileDestPath stringByAppendingPathComponent:@"star.png"];
	
	__block BOOL success;
	ZipArchiveUnpackSuccessBlock successBlock = ^{
		success = YES;
	};
	ZipArchiveUnpackFailureBlock failureBlock = ^{
		success = NO;
	};
	success = NO;
	[ZipArchive unpackFile:archiveFilePath toFolder:archiveFileDestPath successBlock:successBlock failureBlock:failureBlock];
    NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:_timeout];
    while (success == NO && [loopUntil timeIntervalSinceNow] > 0) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil];
    }
	STAssertTrue(success, @"fail unziping file");
	STAssertTrue([_fileManager fileExistsAtPath:unarchivedFile], @"unziped file star.png not exist");

	// Cleaning after test
	// Remove dest folder
	[_fileManager removeItemAtPath:archiveFileDestPath error:NULL];
}

- (void)testUnarchiveFileWithPass
{
	NSCondition *readyCondition = [[NSCondition alloc] init];

	NSString *archivePassFilePath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_PASS_FILE_NAME];
	NSString *archivePassFileDestPath = [TestResourceDirectory stringByAppendingPathComponent:ARCHIVE_PASS_DEST_FOLDER];
	
	__block BOOL success;
	ZipArchiveUnpackSuccessBlock successBlock = ^{
		success = YES;
	};
	ZipArchiveUnpackFailureBlock failureBlock = ^{
		success = NO;
	};
	success = NO;
	[ZipArchive unpackFile:archivePassFilePath toFolder:archivePassFileDestPath usingPassword:@"test" successBlock:successBlock failureBlock:failureBlock];
    NSDate *loopUntil = [NSDate dateWithTimeIntervalSinceNow:_timeout];
    while (success == NO && [loopUntil timeIntervalSinceNow] > 0) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil];
    }
	STAssertFalse(success, @"fail unziping file with wrong password");

	success = NO;
	[ZipArchive unpackFile:archivePassFilePath toFolder:archivePassFileDestPath usingPassword:@"bugaga" successBlock:successBlock failureBlock:failureBlock];
	loopUntil = [NSDate dateWithTimeIntervalSinceNow:30];
    while (success == NO && [loopUntil timeIntervalSinceNow] > 0) {
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:loopUntil];
    }
	STAssertTrue(success, @"fail unziping file");

	// check that file exist
	NSString *unarchivedFile = [archivePassFileDestPath stringByAppendingPathComponent:@"star.png"];
	STAssertTrue([_fileManager fileExistsAtPath:unarchivedFile], @"unziped file star.png not exist");

	// Cleaning after test
	// Remove dest folder
	[_fileManager removeItemAtPath:archivePassFileDestPath error:NULL];
	
	[readyCondition release];
}

@end
